package com.graduate.cec.service;

import com.graduate.cec.common.ResponseCode;
import com.graduate.cec.exception.CecServiceException;
import com.graduate.cec.mapper.CollectMapper;
import com.graduate.cec.pojo.entity.Collect;
import com.graduate.cec.pojo.entity.CollectExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class CollectService {

    @Resource
    private CollectMapper collectMapper;



    /****************        增       ******************************************************************/
    public void insertCollect(Collect collect){

        int i = collectMapper.insertSelective(collect);
        log.info("返回的collect:{}",collect);
        if (i != 1){
            throw new CecServiceException(ResponseCode.INSERT_INFO_ERROR,"新增失败!");
        }

    }












    /****************        删       ******************************************************************/
    public void deleteByKeys(Long id){
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andIdEqualTo(id);
        int i = collectMapper.deleteByExample(collectExample);
        if (i == 0){
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR,"删除失败!");
        }
    }







    /****************        改       ******************************************************************/


    /****************        查       ******************************************************************/

    public List<Collect> getCount(Collect collect){
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(collect.getUserId());
        criteria.andArticleIdEqualTo(collect.getArticleId());
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        
        return collects;
    }

}